/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.audit.service.plugins  
 */
package com.apache.audit.service.plugins;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.audit.entity.LogOperation;
import com.apache.audit.entity.LogOperationDetail;
import com.apache.audit.manager.LogOperationDetailManager;
import com.apache.audit.manager.LogOperationManager;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.tools.DataMap;

/**
 * description:  
 * @author dyhou 创建时间：2016-7-11  
 */
public class LogOperationDetailEntityPluginImpl implements PluginConnector {

	private LogOperationDetailManager logOperationDetailManager;
	private LogOperationManager logOperationManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		String logId = String.valueOf(param.getParams("logId"));
		LogOperation logopation = getLogOpationInfo(logId);
		if (Validator.isNotNull(logId)) {
			Map<String, Object> obj = new HashMap<String, Object>();
			Map item = new HashMap();
			item.put("logType", logopation.getLogType());
			obj.put("item", item);
			ParamsVo<LogOperationDetail> vo = new ParamsVo<LogOperationDetail>();
			LogOperationDetail model = new LogOperationDetail();
			model.setLogId(logId);
			vo.setObj(model);
			vo.setMethodKey("ByService");
			List list = logOperationDetailManager.getList(vo);
			List<Map> result = new ArrayList<Map>();
			if (!Validator.isEmpty(list)) {
				List<LogOperationDetail> oldlist = null;
				LogOperation oldLog = getOldLogOperation(logopation);
				int oldCount = 0;
				if (!Validator.isEmpty(oldLog)) {
					model.setLogId(oldLog.getLogId());
					vo.setObj(model);
					vo.setMethodKey("");
					oldlist = logOperationDetailManager.getList(vo);
					oldCount = oldlist.size();
				}
				for (int i = 0; i < list.size(); i++) {
					Map logVo = (DataMap) list.get(i);
					if ("更新".equals(logopation.getLogType())) {
						if (oldCount == 0) {
							logVo.put("oldValue", logVo.get("propertyValue"));
						} else {
							for (int j = 0; j < oldCount; j++) {
								LogOperationDetail detail = oldlist.get(j);
								if (logVo.get("propertyEname").equals(detail.getPropertyEname())) {
									logVo.put("oldValue", detail.getPropertyValue());
									break;
								}
							}
						}
					}
					result.add(logVo);
				}
				obj.put("total", result.size());
				obj.put("rows", result);
			}
			entity.setEntity(obj);
		}
		entity.setMessage("查询成功");
		return entity;
	}

	private LogOperation getOldLogOperation(LogOperation logopation) {
		if (Validator.isNull(logopation.getPrimaryKey()))
			return null;
		ParamsVo<LogOperation> ovo = new ParamsVo<LogOperation>();
		LogOperation logo = new LogOperation();
		logo.setPrimaryKey(logopation.getPrimaryKey());
		logo.setModelId(logopation.getModelId());
		logo.setCreatedTime(logopation.getCreatedTime());
		ovo.setParams("pageIndex", "1");
		ovo.setParams("pageSize", "2");
		ovo.setObj(logo);
		Page pageList = logOperationManager.getPageInfo(ovo);
		List olist = pageList.getPageObjects();
		if (olist.size() > 1) {
			return (LogOperation) olist.get(1);
		}
		return null;
	}

	private LogOperation getLogOpationInfo(String id) {
		ParamsVo vo = new ParamsVo();
		vo.setInfoId(id);
		LogOperation obj = (LogOperation) logOperationManager.getInfoById(vo);
		return obj;
	}

	public void setLogOperationDetailManager(LogOperationDetailManager logOperationDetailManager) {
		this.logOperationDetailManager = logOperationDetailManager;
	}

	public void setLogOperationManager(LogOperationManager logOperationManager) {
		this.logOperationManager = logOperationManager;
	}

}
